package cn.bigcontainer.alt.workflow.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import cn.bigcontainer.alt.common.util.ExcelUtil;
import cn.bigcontainer.alt.common.util.WoKit;
import cn.bigcontainer.alt.meta.exception.MException;
import cn.bigcontainer.alt.meta.service.MetaService;
import cn.bigcontainer.alt.workflow.service.ImportExcelService;
import cn.bigcontainer.alt.workflow.service.MWorkflowMetaService;
import lombok.extern.slf4j.Slf4j;

/**
 * @author yi ming
 * @version 1.0
 * @date 2020/01/03 上午 9:29
 */
@Service
@Transactional
@Slf4j
public class ImportExcelServiceImpl implements ImportExcelService {

	@Resource
	private MetaService metaService;

	@Resource
	private MWorkflowMetaService workflowMetaService;

	@Resource
	private ApplicationContext context;

	private String getPropsDescData(Map<String, Object> map) {
		String propsDesc = "";
		Object name = map.get("名称");
		Object associatedObjects = map.get("关联对象");
		Object allowedToBeEmpty = map.get("允许为空");
		Object primaryKey = map.get("主键");
		Object type = map.get("类型");
		Object relationalType = map.get("关联类型");
		Object length = map.get("长度");
		Object decimalPlace = map.get("小数位");
		Object defaultValue = map.get("默认值");
		Object range = map.get("范围");
		if (associatedObjects != null) {
			if (relationalType == null) {
				relationalType = "多对一";
			}
			if (name != null) {
				propsDesc += name.toString() + "(";
				propsDesc += "关联:" + associatedObjects.toString() + ",";
				propsDesc += "类型:" + relationalType.toString() + ")";
			} else {
				propsDesc += "关联:" + associatedObjects.toString() + ",";
				propsDesc += "类型:" + relationalType.toString();
			}
		} else {
			String keyValue = "";
			if (allowedToBeEmpty != null) {
				keyValue += "允许为空:" + allowedToBeEmpty.toString() + ",";
			}
			if (primaryKey != null) {
				keyValue += "主键:" + primaryKey.toString() + ",";
			}
			if (type != null) {
				keyValue += "类型:" + type.toString() + ",";
			}
			if (length != null) {
				keyValue += "长度:" + length.toString() + ",";
			}
			if (decimalPlace != null) {
				keyValue += "小数位:" + decimalPlace.toString() + ",";
			}
			if (defaultValue != null) {
				keyValue += "默认值:" + defaultValue.toString() + ",";
			}
			if (range != null) {
				keyValue += "范围:" + range.toString() + ",";
			}
			if ("".equals(keyValue)) {
				try {
					propsDesc += name.toString();
				} catch (Exception e) {
					return "";
				}
			} else {
				propsDesc += name.toString() + "(" + keyValue.substring(0, keyValue.length() - 1) + ")";
			}
		}
		if ("".equals(propsDesc)) {
			return "";
		}
		return propsDesc;
	}

	@Override
	public void importExcel(String moduleName, String objectName, MultipartFile fileContent) {
		if (WoKit.isEmpty(moduleName)) {
			throw new MException(MException.ERROR_NULL, "模块名");
		}
		Workbook book = ExcelUtil.getBookByMultipartFile(fileContent);
		
		if (WoKit.isEmpty(objectName)) {
			// 遍历表。
			for (int i = 0; i < book.getNumberOfSheets(); i++) {
				Sheet sheet = book.getSheetAt(i);
				if (sheet.getSheetName().endsWith("-data")) {
					continue;
				}
				importMeta(moduleName, sheet);
			}
		} else {
			String[] namesStrings = objectName.split("[,，]");
			for (String nameString : namesStrings) {
				importMeta(moduleName, book.getSheet(nameString.trim()));
			}
		}
	}

	private void importMeta(String moduleName, Sheet sheet) {
		// 获取sheet的名字
		String sheetName = sheet.getSheetName();
		List<Map<String, Object>> data = ExcelUtil.getData(sheet);
		String s = "";
		String value = "";
		boolean b = true;
		for (Map<String, Object> map : data) {
			String propsDesc = "";
			Object task = map.get("任务");
			if (task != null) {
				b = false;
				String s1 = task.toString();
				if ("".equals(value)) {
					value = s1;
				}
				if (value.equals(s1)) {
					if ("".equals(s)) {
						s = s1 + "(属性:\"";
					}
					s += getPropsDescData(map);
				} else {
					value = s1;
					if (!"".equals(getPropsDescData(map))) {
						s = s.substring(0, s.length() - 1) + "\");";
						s += s1 + "(属性:\"";
						s += getPropsDescData(map);

					} else {
						propsDesc = s1;
					}
				}
			} else {
				b = true;
				propsDesc = getPropsDescData(map);
			}
			if (!"".equals(s)) {
				s += ";";
			}
			s += propsDesc;
		}
		moduleName = moduleName.replace(",", "");
		if (b) {
			metaService.createMeta(moduleName, sheetName, s);
		} else {
			String replace = s.replace(";;", "\");");
			workflowMetaService.createMeta(moduleName, sheetName, replace);

		}
	}
}
